package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.NoteReceivers;
import com.wsd.smartcarwasher.service.NoteReceiversService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "管理端-通讯消息短信发送配置")
@RestController
@CrossOrigin
public class NoteReceiversController {

    @Resource
    private NoteReceiversService noteReceiversService;

    //新增告警配置
    @ApiOperation(value="添加通讯消息短信发送配置", notes = "告警配置", produces = "application/json", response = ModelMap.class)
    //@ApiImplicitParam(name = "receivers", value = "配置信息", required = true, dataType = "NoteReceivers")
    @RequestMapping(value = "/v1/add_note_receivers", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public ModelMap addNoteReceivers(@RequestBody @ApiParam NoteReceivers receivers) {
        if (receivers == null) {
            return ReturnUtil.Error(ResultState.NO_THING_TO_SAVE.getMsg());
        }
        if (StringUtils.isBlank(receivers.getLevel())) {
            return ReturnUtil.Error(ResultState.EVENT_LEVEL_EMPTY.getMsg());
        }
        NoteReceivers noteReceivers = noteReceiversService.queryReceivers(receivers.getLevel());
        if (noteReceivers != null) {
            return ReturnUtil.Error(ResultState.CONFIG_WITH_SAME_LEVEL_EXIST.getMsg());
        }
        int changeRows = noteReceiversService.addNoteReceivers(receivers);
        if (changeRows == 0) {
            return ReturnUtil.Error(ResultState.ADD_CONFIG_FAIL.getMsg());
        }
        return ReturnUtil.Success(ResultState.ADD_CONFIG_SUCCESS.getMsg(), receivers);
    }

    //修改告警配置
    @ApiOperation(value="修改通讯消息短信发送配置", notes = "告警配置", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "receivers", value = "配置信息", required = true, dataType = "NoteReceivers")
    @RequestMapping(value = "/v1/update_note_receivers", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public ModelMap updateNoteReceivers(@RequestBody NoteReceivers receivers) {
        if (receivers == null) {
            return ReturnUtil.Error(ResultState.NO_THING_TO_SAVE.getMsg());
        }
        if (StringUtils.isBlank(receivers.getLevel())) {
            return ReturnUtil.Error(ResultState.EVENT_LEVEL_EMPTY.getMsg());
        }
        if (StringUtils.isBlank(receivers.getContent()) && StringUtils.isBlank(receivers.getReceivers())) {
            return ReturnUtil.Error(ResultState.NO_THING_TO_SAVE.getMsg());
        }
        int changeRows = noteReceiversService.updateNoteReceivers(receivers);
        if (changeRows == 0) {
            return ReturnUtil.Error(ResultState.UPDATE_CONFIG_FAIL.getMsg());
        }
        return ReturnUtil.Success(ResultState.UPDATE_CONFIG_SUCCESS.getMsg(), receivers);
    }


    //查询指定级别的告警配置
    @ApiOperation(value="查询指定级别的告警配置", notes = "告警配置", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "level", value = "机器事件级别", required = true, dataType = "String")
    @RequestMapping(value = "/v1/note_receivers", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public ModelMap queryReceivers(@RequestParam(value = "level") String level) {
        return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), noteReceiversService.queryReceivers(level));
    }

    @ApiOperation(value="查询所有告警配置", notes = "告警配置", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/all_note_receivers", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public ModelMap queryAllReceivers() {
        return ReturnUtil.Success(ResultState.QUERY_SUCCESS.getMsg(), noteReceiversService.queryAllReceivers());
    }
}
